package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;




    /**
     * 查询用户资料
     * 1.请求头：token
     * 2.请求参数：userID
     */

    @GetMapping
    public ResponseEntity usersMsg(Long userID,@RequestHeader("Authorization")String token){
    /*    boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
          return ResponseEntity.status(401).body(null);
        }*/
        //获取用户id
       /* Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/


        //判断用户id
        if (userID == null){
            userID = UserHolder.getUserId();
        }
       UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }


    /**
     *更新用户资料
     * @param userInfo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization")String token){

     /*   //检查token合法性
        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
*/
        //获取用户id
      /*  Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/

        //设置用户id
       userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);

        //返回值
        return ResponseEntity.ok(null);
    }
}
